﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MenuServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class MenuServices : IMenuServices
    {
        private readonly MenuRepository MenuRepository;

        public MenuServices()
        {
            MenuRepository = new MenuRepository(new WcfSessionStorage());
        }

        #region IMenuServices Members

        public List<MenuDTO> GetList(int page, out long recordCount, string sortProperty = null, string sortType = "ASC")
        {
            var Menus = new List<Menu>();
            var Results = new List<MenuDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Menus = MenuRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Menus = MenuRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = MenuRepository.RecordCount;

            Menus.ForEach(x =>
                              {
                                  var MenuDto = new MenuDTO();
                                  Mapper.Map(x, MenuDto);
                                  //AutoMapper.Mapper.Map(x.ParentMenu, MenuDto.ParentMenu);
                                  Results.Add(MenuDto);
                              }
                );

            return Results;
        }

        public bool Save(MenuDTO MenuDTO)
        {
            bool res;
            try
            {
                Menu Menu = MenuRepository.GetDataOne(x => x.MenuID == MenuDTO.MenuID);
                var user = new User();
                if (Menu == null)
                {
                    Menu = new Menu();
                    MenuDTO.created_date = DateTime.Now;
                }
                else
                {
                    MenuDTO.modified_date = DateTime.Now;
                }
                Mapper.Map(MenuDTO, Menu);
                MenuRepository.SaveOrUpdate(Menu);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(MenuDTO MenuDto)
        {
            bool res;
            try
            {
                var Menu = new Menu();
                Mapper.Map(MenuDto, Menu);
                MenuRepository.Delete(Menu);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<MenuDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                               out long recordCount)
        {
            var Menus = new List<Menu>();
            var Results = new List<MenuDTO>();
            Menus = MenuRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = MenuRepository.RecordCount;
            Menus.ForEach(x =>
                              {
                                  var MenuDto = new MenuDTO();
                                  Mapper.Map(x, MenuDto);
                                  Results.Add(MenuDto);
                              }
                );

            return Results;
        }


        public List<MenuDTO> GetAllMenu()
        {
            List<Menu> Menus = MenuRepository.FetchAll().ToList();
            var Results = new List<MenuDTO>();
            Menus.ForEach(x =>
                              {
                                  var MenuDto = new MenuDTO();
                                  Mapper.Map(x, MenuDto);
                                  Results.Add(MenuDto);
                              }
                );

            return Results.OrderBy(x=>x.MenuText).ToList();
        }


        public MenuDTO GetMenu(int Id)
        {
            Menu Menu = MenuRepository.GetDataOne(x => x.MenuID == Id);
            var MenuDto = new MenuDTO();
            Mapper.Map(Menu, MenuDto);
            return MenuDto;
        }

        #endregion
    }
}